package service

import (
	"server/global"
	// request "server/model/shop/request"
	"server/plugin/amall/model"
	"server/plugin/amall/model/request"
)

type SkuService struct {
}

// CreateSku 创建sku记录
// Author [piexlmax](https://github.com/piexlmax)
func (skuService *SkuService) CreateSku(sku *model.Sku) (err error) {
	err = global.GVA_DB.Create(sku).Error
	return err
}

// DeleteSku 删除sku记录
// Author [piexlmax](https://github.com/piexlmax)
func (skuService *SkuService) DeleteSku(ID string) (err error) {
	err = global.GVA_DB.Delete(&model.Sku{}, "id = ?", ID).Error
	return err
}

// DeleteSkuByIds 批量删除sku记录
// Author [piexlmax](https://github.com/piexlmax)
func (skuService *SkuService) DeleteSkuByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]model.Sku{}, "id in ?", IDs).Error
	return err
}

// UpdateSku 更新sku记录
// Author [piexlmax](https://github.com/piexlmax)
func (skuService *SkuService) UpdateSku(sku model.Sku) (err error) {
	err = global.GVA_DB.Model(&model.Sku{}).Where("id = ?", sku.ID).Updates(&sku).Error
	return err
}

// GetSku 根据ID获取sku记录
// Author [piexlmax](https://github.com/piexlmax)
func (skuService *SkuService) GetSku(ID string) (sku model.Sku, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&sku).Error
	return
}

// GetSkuInfoList 分页获取sku记录
// Author [piexlmax](https://github.com/piexlmax)
func (skuService *SkuService) GetSkuInfoList(info request.SkuSearch) (list []model.Sku, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&model.Sku{})
	var skus []model.Sku
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.GoodID != nil {
		db = db.Where("good_id = ?", *info.GoodID)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&skus).Error
	return skus, total, err
}
